package com.aier.school.action;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.school.common.SchoolAttendanceUtil;
import com.aier.school.common.TeacherLeaveType;
import com.aier.school.entity.SchoolTeacher;
import com.aier.school.entity.TeacherAttendanceMonth;
import com.aier.school.entity.TeacherIcardInfo;
import com.aier.school.entity.TeacherLeave;
import com.aier.school.service.ISchoolLeaveService;
import com.aier.school.service.ISchoolMonAttendService;
import com.aier.school.service.ISchoolTeacherService;

@Scope("prototype")
@Component("schoolAttendDownAction")
public class SchoolAttendDownAction extends SchoolBaseAction {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    @Resource(name = "schoolMonAttendService")
    private ISchoolMonAttendService schoolMonAttendService;
    @Resource(name = "schoolLeaveService")
    private ISchoolLeaveService schoolLeaveService;
    @Resource(name = "schoolTeacherService")
    private ISchoolTeacherService schoolTeacherService;
    
    
    private int sid;                //学校编号
    private String account;            //教师账号
    private Long gid;                //职务id
    private String monthBeginValue;    //开始年月
    private String monthEndValue;    //结束年月
    
    private ByteArrayInputStream inputExcel;
    private String filename;    
    
 
    @Override
    public String execute(){
        return SUCCESS;
    }

    public String down() {
        Integer year_end = null,month_end = null,year_begin = null,month_begin = null;
        List<TeacherAttendanceMonth> attMonthList;
        List<TeacherIcardInfo> attDetailList;
        List<TeacherLeave> terLeaveList;
        if (monthBeginValue != null && !monthBeginValue.equals("")) {
            String[] st = monthBeginValue.split("-");
            year_begin = Integer.parseInt(st[0]);
            month_begin = Integer.parseInt(st[1]);
        }
        if (monthEndValue != null && !monthEndValue.equals("")) {
            String[] st = monthEndValue.split("-");
            year_end = Integer.parseInt(st[0]);
            month_end = Integer.parseInt(st[1]);
        }
        List<Long> tids = new ArrayList<Long>();    
        List<SchoolTeacher> teachers = schoolTeacherService.selectTeacher(sid,account,gid);
        if(teachers!=null&&teachers.size()!=0){
            for(SchoolTeacher teacher:teachers){
                tids.add(teacher.getTid());
            }
            attMonthList = schoolMonAttendService.searchAttMonthsByCondition(sid,tids,
                    year_begin,month_begin,year_end,month_end);
            attDetailList = schoolMonAttendService.searchAttDetailsByCondition(sid,tids,
                    year_begin,month_begin,year_end,month_end);
            terLeaveList = schoolLeaveService.searchTerLeavesByCondition(sid,tids,
                    TeacherLeaveType.TYPE_HABDLE_BJ,year_begin,month_begin,year_end,month_end);
        }else{
            attMonthList = null;
            attDetailList = null;
            terLeaveList = null;
        }
        
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            SchoolAttendanceUtil.createExcel(attMonthList,attDetailList,terLeaveList).write(out);
        } catch (IOException e) {
            return null;
        } catch (Exception e){
            e.printStackTrace();
        }
        inputExcel = new ByteArrayInputStream(out.toByteArray());
        filename="school_attendance.xls";
        return SUCCESS;        
    }
    
    
    
    
    public ByteArrayInputStream getInputExcel() {
        return inputExcel;
    }

    public String getFilename() {
        return filename;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public int getSid() {
        return sid;
    }

    public void setSchoolMonAttendService(
            ISchoolMonAttendService schoolMonAttendService) {
        this.schoolMonAttendService = schoolMonAttendService;
    }

    public void setSchoolLeaveService(ISchoolLeaveService schoolLeaveService) {
        this.schoolLeaveService = schoolLeaveService;
    }

    public void setSchoolTeacherService(ISchoolTeacherService schoolTeacherService) {
        this.schoolTeacherService = schoolTeacherService;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public void setGid(Long gid) {
        this.gid = gid;
    }

    public void setMonthBeginValue(String monthBeginValue) {
        this.monthBeginValue = monthBeginValue;
    }

    public void setMonthEndValue(String monthEndValue) {
        this.monthEndValue = monthEndValue;
    }

    public void setInputExcel(ByteArrayInputStream inputExcel) {
        this.inputExcel = inputExcel;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }



}
